package com.github.lock;

import java.util.Stack;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Description:
 *
 * @author zwl
 * @version 1.0
 * @date 2021/8/16 下午7:47
 */
public class TestReentrantLock {

    static int count = 0;

    final static int THREAD_NUM = 1;

    public static void main(String[] args) throws InterruptedException {

        // 使用公平锁
        //ReentrantLock lock = new ReentrantLock(true);

        Semaphore semaphore = new Semaphore(2);


        CountDownLatch latch = new CountDownLatch(THREAD_NUM);

        for (int i = 0;i < THREAD_NUM;i++){
            new Thread(() -> {
                //System.out.println(Thread.currentThread().getName() + " try to get lock...");
                //lock.lock();
                //try{
                //    System.out.println(Thread.currentThread().getName() + " get lock success, run...");
                //try {
                //    TimeUnit.SECONDS.sleep(60 * 60);
                //} catch (InterruptedException e) {
                //    e.printStackTrace();
                //}
                //    count++;
                //} catch (InterruptedException e) {
                //    e.printStackTrace();
                //} finally {
                //    lock.unlock();
                //    System.out.println(Thread.currentThread().getName() + " un lock!!!");
                //    latch.countDown();
                //}
                latch.countDown();
            }).start();
        }

        latch.await();
        System.out.println(count);
    }
}
